package org.looom.orglooomschool;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main_sharedata1 extends AppCompatActivity {
    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button buttonRegister;
    private Button buttonLogin;
    private TextView textViewStatus;

    // SharedPreferences的文件名
    private static final String PREFS_NAME = "UserPrefs";
    // 存储用户名和密码的key
    private static final String KEY_USERNAME = "username";
    private static final String KEY_PASSWORD = "password";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharedata1);

        // 获取布局中的控件实例
        editTextUsername = findViewById(R.id.editTextUsername);
        editTextPassword = findViewById(R.id.editTextPassword);
        buttonRegister = findViewById(R.id.buttonRegister);
        buttonLogin = findViewById(R.id.buttonLogin);
        textViewStatus = findViewById(R.id.textViewStatus);

        // 设置注册按钮的点击监听器
        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerUser();
            }
        });

        // 设置登录按钮的点击监听器
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginUser();
            }
        });
    }

    // 注册用户方法
    private void registerUser() {
        String username = editTextUsername.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();

        // 检查用户名和密码是否为空
        if (username.isEmpty() || password.isEmpty()) {
            textViewStatus.setText("用户名或密码不能为空");
            return;
        }

        // 获取SharedPreferences对象
        SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        // 创建SharedPreferences编辑器对象
        SharedPreferences.Editor editor = sharedPreferences.edit();

        // 将用户名和密码存入SharedPreferences
        editor.putString(KEY_USERNAME, username);
        editor.putString(KEY_PASSWORD, password);

        // 提交修改 (异步)
        editor.apply();

        textViewStatus.setText("注册成功");
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
    }

    // 登录用户方法
    private void loginUser() {
        String username = editTextUsername.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();

        // 检查用户名和密码是否为空
        if (username.isEmpty() || password.isEmpty()) {
            textViewStatus.setText("请输入用户名和密码");
            return;
        }

        // 获取SharedPreferences对象
        SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        // 从SharedPreferences读取存储的用户名和密码
        String storedUsername = sharedPreferences.getString(KEY_USERNAME, null);
        String storedPassword = sharedPreferences.getString(KEY_PASSWORD, null);

        // 检查是否已注册且用户名密码是否匹配
        if (storedUsername != null && storedPassword != null && username.equals(storedUsername) && password.equals(storedPassword)) {
            textViewStatus.setText("登录成功");
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

            // 创建Intent并跳转到文件操作界面
            Intent intent = new Intent(Main_sharedata1.this, Main_sharedata2.class);
            startActivity(intent);
        } else {
            textViewStatus.setText("登录失败，用户名或密码错误，或用户未注册");
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
        }
    }
}
